/**
 * 粘贴板复制
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var angular = require('angular');
    require('lib/zeroclipboard/ZeroClipboard.js');
    var expanderModule = angular.module('clipboard', [])

    expanderModule.directive('clipboard', ['$parse', function ($parse) {
        return {
            restrict: 'EA',
            replace: true,
            transclude: false,
            scope: {
                data: '@',
                aftercopy: "&"
            },
            link: function ($scope, element, attrs) {
                var zeroclipboard = window['ZeroClipboard'];
                var clipboard = new zeroclipboard(element);
                clipboard.on("ready", function (readyEvent) {
                    clipboard.on("beforecopy", function (event) {
                        clipboard.setData("text/plain", $scope.data);
                    });

                    clipboard.on("aftercopy", function (event) {
                        setTimeout(function () {
                            var call = $parse($scope.aftercopy)
                            $scope.$apply(function () {
                                call() && call()();
                            });

                        }, 0);
                    });

                    clipboard.on('error', function (event) {
                        zeroclipboard.destroy();
                    })
                });
            }
        }
    }]);

});
